package kanboard

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/entity"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/db"
	"time"
)

/**
扁平化的数据库模型
*/
type kanBoard struct {
	OpinionSum  int `xorm:"opinionsum"`  //留言总数
	OpinionFlow int `xorm:"opinionflow"` //留言分配
	OpinionAck  int `xorm:"opinionack"`  //留言
	OpinionFin  int `xorm:"opinionfin"`  //留言办结

	OpiniontypeSpecies       int `xorm:"opinion_type_species"`       //留言类型-品种类
	OpiniontypeBusiness      int `xorm:"opinion_type_business"`      //留言办结-业务类
	OpiniontypeTechnology    int `xorm:"opinion_type_technology"`    //留言办结-技术类
	OpiniontypeCurbplatform  int `xorm:"opinion_type_curbplatform"`  //留言办结-场外平台类
	OpiniontypeMarketservice int `xorm:"opinion_type_marketservice"` //留言办结-市场服务类

	OpiniontypeOrganization int `xorm:"organization"` //用户类型-机构
	OpiniontypePersonal     int `xorm:"personal"`     //用户类型-个人
}

/**
获取看板数据
*/
func GetKanboardInfo(start, end time.Time) (*entity.KanBoard, error) {
	sessionInstance := db.Session()
	defer sessionInstance.Close()

	kanboard := entity.KanBoard{}
	_kanboard := kanBoard{}

	sql := `select
    count(*) opinionsum,
    count( case when w.status  = 1 then "" end) opinionflow,
    count( case when w.status = 2 then "" end) opinionack,
    count( case when w.status = 3 then "" end) opinionfin,
	count( case when opinion_type = 1 then "" end) opinion_type_species,
	count( case when opinion_type = 2 then "" end) opinion_type_business,
	count( case when opinion_type = 3 then "" end) opinion_type_technology,
	count( case when opinion_type = 4 then "" end) opinion_type_curbplatform,
	count( case when opinion_type = 5 then "" end) opinion_type_marketservice,
    count( case when c.is_organization = 1 then "" end) organization,
    count( case when c.is_organization = 0 then "" end) personal
	from
    work_order w
    left join customer c on c.id = w.customer_id`
	//时间限制
	timeLimit := "where w.created_at >= '" + start.String() + "' and w.created_at <='" + end.String() + "';"

	_, err := sessionInstance.SQL(sql + timeLimit).
		Get(&_kanboard)
	if err != nil {
		return &kanboard, err
	}
	//拼装
	kanboard.OpinionSum = _kanboard.OpinionSum
	kanboard.OpinionFlow = _kanboard.OpinionFlow
	kanboard.OpinionAck = _kanboard.OpinionAck
	kanboard.OpinionFin = _kanboard.OpinionFin

	kanboard.OpinionType = append(kanboard.OpinionType, entity.OpinionKV{Name: "品种类", Count: _kanboard.OpiniontypeSpecies})
	kanboard.OpinionType = append(kanboard.OpinionType, entity.OpinionKV{Name: "业务类", Count: _kanboard.OpiniontypeBusiness})
	kanboard.OpinionType = append(kanboard.OpinionType, entity.OpinionKV{Name: "技术类", Count: _kanboard.OpiniontypeTechnology})
	kanboard.OpinionType = append(kanboard.OpinionType, entity.OpinionKV{Name: "场外平台类", Count: _kanboard.OpiniontypeCurbplatform})
	kanboard.OpinionType = append(kanboard.OpinionType, entity.OpinionKV{Name: "市场服务类", Count: _kanboard.OpiniontypeMarketservice})

	kanboard.CustomerType = append(kanboard.CustomerType, entity.OpinionKV{Name: "个人", Count: _kanboard.OpiniontypeOrganization})
	kanboard.CustomerType = append(kanboard.CustomerType, entity.OpinionKV{Name: "机构", Count: _kanboard.OpiniontypePersonal})
	return &kanboard, nil
}
